<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<%@ include file="/pub/head_latest.inc"%>
    	<title></title>
	</head>
	<style type="text/css">
		img{
			width:16px !important;
		}
		
		.l-panel-btn{
			float: right;
		}
		
		
		.l-grid-totalsummary{
			background: none;
		}
		
		.l-grid-totalsummary td {
		    line-height: 35px;
		}
	</style>
	<script src="${baseURL}/js/calculate.js" type="text/javascript"></script>
	<script>
		 var calculate = new Calculate();
	</script>
	<body>
  		<!-- 搜索表单 -->
  		<div class="mainsearch" style="width: 98%">
			<div class="search-header">${report.moduleName}</div>
			<div class="searchbox">
				<%@ include file="../selector_form.jsp"%>
				<form id="formsearch" method="post">
					<input type="hidden" name="list" id="list" class="field" />
					<div class="selector-form"></div>
				</form>
			</div>
		</div>
		<div id="maingrid"></div>
		
		<!-- 报表打印区域 start -->
  		<iframe id="exportFrame" style="display:none" name="exportFrame" src="${baseURL}/report/simplereport/simpleReport.do?csId=${drawForm.id}&rand=Math.rand()"></iframe>
  		 <script>
  		 	var csId = '${drawForm.id}';
  		 	function bulidFilterParams(){
  		 		var data = getSelectorValue();
  		 		data = {'searchParam':JSON.stringify(data)};
  		 		return data;
  		 	}
  		 	
			function FF_Print(){
				var json = bulidFilterParams();
  				exportFrame.window.FF_Print(json);
 			}
			
			function FF_ExportExcel(){
				var json = bulidFilterParams();
 				exportFrame.window.FF_ExportExcel(json);
 			}
			
			function FF_ExportDoc(){
				var json = bulidFilterParams();
 				exportFrame.window.FF_ExportDoc(json);
			}
			
			function FF_ExportPDF(){
				var json = bulidFilterParams();
				exportFrame.window.FF_ExportPDF(json);
			}
		</script>
		<!-- 报表打印区域 end -->
		<!-- 钻取的配置 start -->
		<script>
 			function buildURLParams(json,url){
 				var params = url+"&";
				var temp = "";
				for(var key in json){
					temp = key+"="+json[key];
					params += temp;
				}
				return params;
			}
		
			function FF_openDialog(data){
 				  $.ligerDialog({
					width:800,
					height:600,
					url:buildURLParams(data.json,data.url)
				});  
			}
			
			function FF_openTab(data){
  		 		parent.f_addTab(data.linkCode,data.linkName,buildURLParams(data.json,data.url));
			}
			
			function FF_openWindow(data){
				//设置弹窗大小
				var height = screen.height, // 用于计算弹出窗口的高度
					width  = screen.width - 80;  //用于计算弹出窗口的宽度
		  		window.open(buildURLParams(data.json,data.url),"newwindow","scrollbars=yes,height="+height+", width="+width+", top=0,left=60");
 			}
		</script>
		<!-- 钻取的配置 end -->
		 
  		<script type="text/javascript">
  		/*
  		 	解析获取最终替换数值的表达式
  		*/
  		function dispart(expression,row){
		    var tempExpression = expression;
		    var regex = /\{(.+?)\}/ig;	
		    var r = "";
		    var temp = "";
			    while(r = regex.exec(expression)) {   
				  tempExpression = tempExpression.replace(r[0],row[r[1]] == undefined ?"":row[r[1]]);
		    }  
		    return tempExpression;
		}
  		
      	  //递归产生叶级列表结构
      	    function genChildColumn(columnObj){
       	    	var childrenColumns = [];
      	    	
      	    	if(columnObj.hasChidren){
      	    		for(var i = 0 ; i < columnObj.children.length ; i++){
      	      	    	var temp = {};
      	      	    	var tempObj = null;
      	    			tempObj = (columnObj.children)[i];
      	    			temp.display = tempObj.displayName;
      	    			temp.name = tempObj.fieldName;
      	    			temp.type = tempObj.type || "text";
      	    			temp.width = 120;
      	    		 	if(tempObj.link){
      	    		 		var linkForm = tempObj.linkForm;
      	    		 	  
      	    		 		temp.render = function(record, rowindex, value, column){
      	    		 			 var json = {};
      							 for(var i = 0 ; i < linkForm.linkParams.length ; i++){
     								 if(linkForm.linkParams[i].valueFieldName != null && linkForm.linkParams[i].valueFieldName!=''){
    									 json[linkForm.linkParams[i].fieldName] = record[linkForm.linkParams[i].valueFieldName];
    								 }else{
    									 json[linkForm.linkParams[i].fieldName] = linkForm.linkParams[i].value;
    								 }
    							 }
  	      	    		 		 var data = {
	     		      				    url:"${baseURL}/"+linkForm.linkURL+"" ,
	     		      					json:json,
	     		      					linkCode:""+linkForm.linkCode+"",
	     		      					linkName:""+linkForm.linkName+""
	     		      			};
	      	    		 		  if(value != undefined ){
	    	  		      			if(linkForm.openType == 0){//TAB
	    		 	      				return "<a href='javascript:FF_openTab("+JSON.stringify(data)+");'>"+value+"</a>";
	    			      			}else if(linkForm.openType == 1 ){// new window
	    		 	      				return "<a href='javascript:FF_openWindow("+JSON.stringify(data)+");'>"+value+"</a>";
	    			      			}else if(linkForm.openType == 2 ){// 弹出框
	    		 	      				return "<a href='javascript:FF_openDialog("+JSON.stringify(data)+");'>"+value+"</a>";
	    			      			}else{
	    		 	      				return "<a href='javascript:FF_openTab("+JSON.stringify(data)+");'>"+value+"</a>";
	    			      			}
	    		      			} 
	    	      				return value; //返回此单元格显示的HTML内容(一般根据value和row的内容进行组织)
       	    		 		}
      	    		 	}
      	    		 	
      	    		 	if(tempObj.useValueExpression){
      	    		 		temp.render = function(record, rowindex, value, column){
      	    		 			var valueExpression = dispart(tempObj.valueExpression,record);
      		      			 	calculate.setExpression(valueExpression);
      		      			 	return calculate.calculateResult(); 
      	    		 		}
      	    		 	}
      	    		 	
      	    		 	if(tempObj.hasChidren){
      	    		 		temp.columns = genChildColumn(tempObj);
      	    		 	}
      	    			childrenColumns[i] = temp;
          	    	}
       	    	}
       	    	return childrenColumns;
      	    }
      	  
	      	var columns = [];
	        <c:forEach var="o" items="${drawForm.results}" varStatus="idx">
  	      		var column_${idx.index} = {};
	      		column_${idx.index}.display = '${o.displayName}';
	      		column_${idx.index}.name = '${o.fieldName}';
	      		column_${idx.index}.type = '${o.type}' || 'text';
	      		column_${idx.index}.width = 120;
	      		
	      		<c:if test="${o.hasChidren}">
	      			column_${idx.index}.columns = genChildColumn(${o.column});
	      		</c:if>
	      		
 	      		  <c:if test="${o.link || o.useValueExpression}">
 	      			column_${idx.index}.render = function (record, rowindex, value, column) {
	      			 <c:if test="${o.useValueExpression}">
	      			 	var valueExpression = dispart('${o.valueExpression}',record);
	      			 	calculate.setExpression(valueExpression);
	      			 	return calculate.calculateResult(); 
	      			 </c:if>
	      			 <c:if test="${o.link}">
	      				//this 这里指向grid， record 行数据 ,rowindex 行索引，value 当前的值，对应record[column.name],column 列信息
	      				var json = {};
	    	        	  <c:forEach var="op" items="${o.linkForm.linkParams}">
	    	        	  	<c:choose>
	    	        	  		<c:when test="${op.valueFieldName != null}">
	    	        	  			json.${op.fieldName} = record.${op.valueFieldName};
	    	        	  		</c:when>
	    	        	  		<c:otherwise>
	    	        	  			json.${op.fieldName} = "${op.value}";
	    	        	  		</c:otherwise>
	    	        	  	</c:choose>
						</c:forEach>  
		      			 var data = {
		      				    url:"${baseURL}${o.linkForm.linkURL}",
		      					json:json,
		      					linkCode:"${o.linkForm.linkCode}",
		      					linkName:"${o.linkForm.linkName}"
		      			};
		      			if(value != undefined ){
	  		      			if(${o.linkForm.openType == 0}){//TAB
		 	      				return "<a href='javascript:FF_openTab("+JSON.stringify(data)+");'>"+value+"</a>";
			      			}else if(${o.linkForm.openType == 1}){// new window
		 	      				return "<a href='javascript:FF_openWindow("+JSON.stringify(data)+");'>"+value+"</a>";
			      			}else if(${o.linkForm.openType == 2}){// 弹出框
		 	      				return "<a href='javascript:FF_openDialog("+JSON.stringify(data)+");'>"+value+"</a>";
			      			}else{
		 	      				return "<a href='javascript:FF_openTab("+JSON.stringify(data)+");'>"+value+"</a>";
			      			}
		      			}
	      				return value; //返回此单元格显示的HTML内容(一般根据value和row的内容进行组织)
	      			 </c:if>
	      			};
	      		</c:if>  
	      		columns[${idx.index}] = column_${idx.index};
	         </c:forEach> 
	         
  	      	var grid = $("#maingrid").ligerGrid({ 
	      		columns: columns,
				dataAction: 'server',<c:if test="${drawForm.print || drawForm.pdf || drawForm.doc || drawForm.excel}">toolbar:{},</c:if>
	          	sortName: 'code',
	          	width: '98%', height: '100%',heightDiff:-10, checkbox: true,usePager:${usePage},
	          	pageParmName:"pageNo",pageSizeOptions:[${pageOptions}],pagesizeParmName:"perPageSize",
	          	rowHeight:23,rownumbers:true,isScroll:true,sortnameParmName:"sortName",
	          	sortorderParmName:"sortOrder",checkbox:true,pageSize: ${pageSize}
	      	});
  	      	
  	      	var arr = [];
  	  	 	<c:if test="${drawForm.print}">
  	 		 	arr.push({id : "print", name : "打印", clickEvent : FF_Print, icon : "a fa fa-print" ,type:"icon"});
  	  	 	</c:if>
  	  	 	<c:if test="${drawForm.excel}">
  		 		arr.push({id : "excel", name : "导出Excel", clickEvent : FF_ExportExcel,  icon : "a fa fa-file-excel-o" ,type:"icon"});
  		 	</c:if>
  		 	<c:if test="${drawForm.doc}">
  	 			arr.push({id : "doc", name : "导出Word", clickEvent : FF_ExportDoc,  icon : "a fa fa-file-word-o" ,type:"icon"});
  		 	</c:if>
  		 	<c:if test="${drawForm.pdf}">
  				arr.push({id : "pdf", name : "导出PDF", clickEvent : FF_ExportPDF, icon : "a fa fa-file-pdf-o" ,type:"icon"});
  		 	</c:if>
  	      	
  	      	if(arr.length > 0){
  	   	      	LG.createToolbarButton(grid, arr);
  	      	}
 
	      	//一个重新加载数据方法，该方法主要作为外部框架调用使用
	      	function FF_Reload(){
				grid.loadData();
	      	}
	      	
	      	$(".mainsearch>.search-header").click(function(){
		   		 if($(".searchbox").is(":visible")){
		   			 $(".searchbox").hide();
		   			 grid._onResize();
		   		 }else{
		   			 $(".searchbox").show();
		   			 grid._onResize();
		   		 }
		   	});
		</script>
		<script type="text/javascript">
		 var exams = {};
		 var sclass = {};
		 var subject = {};
		 var selectorData = JSON.parse('${report.selectorData}'||"[]",function(k,v){
			  if(v.indexOf&&v.indexOf('function')>-1){
			     return eval("(function(){return "+v+" })()");
			  }
			  return v;
		 });
		 var defaultData = JSON.parse('${report.defaultData}'||"[]",function(k,v){
			  if(v.indexOf&&v.indexOf('function')>-1){
			     return eval("(function(){return "+v+" })()");
			  }
			  return v;
		 });
		 
		 initData($(".selector-form"),selectorData,defaultData,'${module.module}');
		 
		 //渲染选择器
	  	 renderSelector(getDefaultSelector());
		 
	  	 for(var j = 0; j < defaultData.length; j++){
			 if(defaultData[j].id=="exam_id"){
				 $("li[selector-id='term_id']").hide();
				 $(".searchbox").hide();
				 break;
			 }
		 }
		 var searchCondition = JSON.parse('${report.searchCondition}'||"[]");
		 for (var i = 0; i < searchCondition.length; i++) {
			 var each = searchCondition[i];
			 if(each.data){
				 setSearchContainer(each.id, each.name, each.data);
			 }else if(each.url){
			  	 $.ajax({
					 type: "POST",
					 async : false,
					 url: baseURL + "/" + each.url,
					 success: function(data){
						 if(data){
							 setSearchContainer(each.id, each.name, data);
							 if(each.id == "term_id"){
								 renderNewSelector("term_id",data[data.length-1].id);
								 loadExam(data[data.length-1].id);
							 }
						 }
					 }
			 	 });
			 }
	   }
		 
	   initSearchGrid(grid,'${baseURL}${report.listJSONURL}?csId=${drawForm.id}');
	
	   function beforeListExam(){
		   var termId = $("button[data-id='term_id']").attr("data-value");
		   if(exams && exams.hasOwnProperty(termId)){
		  	   setSearchContainer("exam_id", "考试", exams[termId]);
		   }else{
		  	   loadExam(termId);
		   }
	   }
	   
	   function afterTermSelected(value){
		   loadExam(value);
	   }
	   
	   function afterExamSelected(value){
		   loadClass(value);
	   }
	   
	   function beforeListClass(value){
		   var examId = $("button[data-id='exam_id']").attr("data-value");
		   if(!examId){
			   examId = '${param.exam_id}';
		   }
		   if(sclass && sclass.hasOwnProperty(examId)){
			   for(var key in selectorData){
					if((selectorData[key].id).indexOf("class_id")!=-1){
						setSearchContainer(selectorData[key].id, "班级", sclass[examId]);
						break;
					}
			   }
		   }else{
			   loadClass(examId);
		   }
	   }
	   
	   function beforeListSubject(value){
		   var examId = $("button[data-id='exam_id']").attr("data-value");
		   if(!examId){
			   examId = '${param.exam_id}';
		   }
		   if(subject && subject.hasOwnProperty(examId)){
		  	   for(var key in selectorData){
					if((selectorData[key].id).indexOf("subject_id")!=-1){
						setSearchContainer(selectorData[key].id, "科目", subject[examId]);
						break;
					}
			   }
		   }else{
			   loadSubject(examId);
		   }
	   }
	   
	   function loadExam(termId){
		   var url = "/exam/exam/examlist/listJSONDataAll.do";
		   for (var i = 0; i < searchCondition.length; i++) {
				 var each = searchCondition[i];
				 if(each.id == "exam_id" && each.url){
					 url = each.url;
				 }
		   }
	  	   $.ajax({
				type: "POST",
				async : false,
				url: baseURL + url,
				data:{termId:termId,isMine:0},
				success: function(data){
					if(data){
						setSearchContainer("exam_id", "考试", data);
						if(data.length>0){
							renderNewSelector("exam_id",data[data.length-1].id);
							exams[termId] = data;
							search();
						}
					}
				}
		 	});
	   	}
	   
	   	function loadClass(examId){
		  	 $.ajax({
					type: "POST",
					async : false,
					url: baseURL + "/sclass/classlist/listJSONDataByExam.do",
					data:{examId:examId},
					success: function(data){
						if(data){
							sclass[examId] = data;
							for(var key in selectorData){
								if((selectorData[key].id).indexOf("class_id")!=-1){
									setSearchContainer(selectorData[key].id, "班级", data);
									break;
								}
							}
						}
					}
			 	});
		   }
	   	
	   	function loadSubject(examId){
	   		var url = baseURL + "/exam/exam/examlist/listSubject.do";
 	   		for(var key in searchCondition){
	   			if((searchCondition[key].id).indexOf("subject_id")!=-1){
	   				url = baseURL + "/" + searchCondition[key].url;
	   			}
	   		}
		  	 $.ajax({
					type: "POST",
					async : false,
					url: url,
					data:{examId:examId},
					success: function(data){
						if(data){
							subject[examId] = data;
							for(var key in selectorData){
								if((selectorData[key].id).indexOf("subject_id")!=-1){
									setSearchContainer(selectorData[key].id, "科目", data);
									break;
								}
							}
						}
					}
			 	});
		   }
		</script>
	</body>
</html>
